#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::video_init()
{
    this->video_buffer = new char[640 * 480 * 3];
    this->video_rgbbuf = new char[640 * 480 * 3];
    video_size = 0;
    video_v4l2 = nullptr;
    video_timer = new QTimer;

    connect(video_timer, &QTimer::timeout,
            this, &MainWindow::video_play_Ui);
    video_timer->setInterval(video_refresh_rate);
}

void MainWindow::video_start()
{
    if(video_v4l2 == nullptr)
    {
        video_v4l2 = new V4l2Api("/dev/video7");
         video_v4l2->video_start(); //开启摄像头
    }
    //同步开启定时器或者子线程
    video_timer->start();
}

void MainWindow::video_stop()
{
    video_timer->stop();
    if(video_v4l2 != nullptr)
    {
        video_v4l2->video_stop();
        video_v4l2->video_destory();
        video_v4l2 = nullptr;
    }
}

void MainWindow::video_play_Ui()
{
    video_v4l2->get_getframe(video_buffer,&video_size);
    video_v4l2->yuv422torgb24((unsigned char*)video_buffer,(unsigned char*)video_rgbbuf,640,480);

    //QImage(uchar *data, int width, int height, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr)
    QImage image = QImage((unsigned char*)video_rgbbuf,640,480,QImage::Format_RGB888);

    QPixmap map =  QPixmap::fromImage(image);
    map = map.scaled(320, 240, Qt::KeepAspectRatio, Qt::SmoothTransformation);//等比压缩
    map = map.scaled(ui->label_video->width(),ui->label_video->height());
    ui->label_video->setPixmap(map);
}
